using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;

public partial class Get_Thumbnail : System.Web.UI.Page
{
    private string f;
    private int w;
    private int h;

    protected void Page_Load(object sender, System.EventArgs e)
    {

       

        if (Request["f"] != null)
        {
            f = Request["f"];
            f = f.Replace("'", "");
        }
        else
        {
            f = "";
        }
        if (Request["w"] != null)
        {
            w = Convert.ToInt32(Request["w"]);
        }
        else
        {
            w = 96;
        }
        if (Request["h"] != null)
        {
            h = Convert.ToInt32(Request["h"]);
        }
        else
        {
            h = 82;
        }

        int intRatioHeight;
        //Ratio Height 
        int intRatioWidth;
        //Ratio Width 
        //get the file name -- fall800.jpg 
        string File = f;
        //File = "/images/" & Request.QueryString("s").Replace("http://", "").Replace("https://", "").Replace("www.", "") & "/" & Request.QueryString("f") 

        System.Drawing.Image Image;
        try
        {
            //create an image object, using the filename we just retrieved 
            Image = System.Drawing.Image.FromFile(Server.MapPath(File));


            //Set Ratio height and width 

            intRatioWidth = w;
            intRatioHeight = h;


            ImageRatio(Image,ref intRatioWidth,ref intRatioHeight);

            //create the actual thumbnail image 
            System.Drawing.Image ThumbnailImage = Image.GetThumbnailImage(intRatioWidth, intRatioHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

            //make a memory stream to work with the image bytes 
            System.IO.MemoryStream ImageStream = new System.IO.MemoryStream();

            //put the image into the memory stream 
            ThumbnailImage.Save(ImageStream, System.Drawing.Imaging.ImageFormat.Jpeg);

            //make byte array the same size as the image 
            byte[] ImageContent = new byte[ImageStream.Length + 1];

            //rewind the memory stream 
            ImageStream.Position = 0;

            //load the byte array with the image 
            ImageStream.Read(ImageContent, 0,Convert.ToInt32(ImageStream.Length));

            //return byte array to caller with image type 

            Response.ContentType = "image/gif";
            Response.BinaryWrite(ImageContent);
        }

        catch (Exception ex)
        {
        }

    }

    public bool ThumbnailCallback()
    {
        return true;
    }

    protected void ImageRatio(System.Drawing.Image img, ref int Width, ref int Height)
    {

        if ((img.Width <= Width) && (img.Height <= Height))
        {
            Width = img.Width;
            Height = img.Height;
        }

        else if (img.Width > Width | img.Height > Height)
        {
            double widthRatio = (double)img.Width / (double)Width;
            double heightRatio = (double)img.Height / (double)Height;
            decimal ratio = (decimal)Math.Max(widthRatio, heightRatio);
            Width = (int)(img.Width / ratio);
            Height = (int)(img.Height / ratio);
           
        }

    } 

  
}
